package controller

import (
	"gin-shop-admin/models"
	"gin-shop-admin/repository"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"time"
)

func ProductList(c *gin.Context) {
	page, page_err := c.GetQuery("page")
	limit, limit_err := c.GetQuery("limit")
	if !page_err || !limit_err {
		productList, err := repository.GetAllProducts()
		if err != nil {
			c.JSON(201, gin.H{"error": err.Error()})
		} else {
			c.JSON(http.StatusOK, productList)
		}
	} else {
		pageint, _ := strconv.Atoi(page)
		limitint, _ := strconv.Atoi(limit)
		total, productList, err := repository.GetProducts(pageint, limitint)
		if err != nil {
			c.JSON(202, err)
			return
		} else {
			data := gin.H{
				"total": total,
				"listQuery": map[string]int{
					"page":  pageint,
					"limit": limitint,
				},
				"list": productList,
			}
			c.JSON(http.StatusOK, data)
			return
		}
	}
}

func ProductGet(c *gin.Context) {
	id, ok := c.GetQuery("id")
	if !ok {
		c.JSON(http.StatusCreated, "获取参数ID错误")
	} else {
		product, err := repository.GetProductByID(id)
		if err != nil {
			c.JSON(http.StatusCreated, "商品ID获取不到数据")
		} else {
			c.JSON(http.StatusOK, product)
		}
	}
}

func ProductUpdate(c *gin.Context) {
	id, ok := c.Params.Get("id")
	if !ok {
		c.JSON(201, gin.H{"error": "无效的id"})
		return
	}
	product, err := repository.GetProductByID(id)
	if err != nil {
		c.JSON(202, gin.H{"error": err.Error()})
		return
	}
	if err = c.ShouldBind(&product); err != nil {
		c.JSON(203, gin.H{"error": err.Error()})
	} else {
		product.Update_time = time.Now()
		product.Image = "static/upload/product/" + id + ".png"
		if err = repository.UpdateOneProduct(product); err != nil {
			c.JSON(204, gin.H{"error": err.Error()})
		} else {
			c.JSON(http.StatusOK, gin.H{"msg": "success"})
		}
	}
	return
}

func ProductAdd(c *gin.Context) {
	name := c.PostForm("name")
	price := c.PostForm("price")
	sale_price := c.PostForm("sale_price")
	sale_point := c.PostForm("sale_point")
	type_name := c.PostForm("type_name")
	flag := c.PostForm("flag")
	price_int, _ := strconv.Atoi(price)
	sale_price_int, _ := strconv.Atoi(sale_price)
	flag_int, _ := strconv.Atoi(flag)
	product := &models.Product{
		Name:        name,
		Image:       "/static/upload/product/p30.jpg",
		Price:       price_int,
		Sale_price:  sale_price_int,
		Sale_point:  sale_point,
		Type_name:   type_name,
		Type_id:     1,
		Flag:        flag_int,
		Create_time: time.Now(),
		Update_time: time.Now(),
		Create_user: 1,
		Update_user: 2,
	}
	err := repository.InsertOneProduct(product)
	if err != nil {
		c.JSON(201, gin.H{"error": err.Error()})
		return
	} else {
		c.JSON(http.StatusOK, gin.H{"msg": "success"})
		return
	}
}

func ProductDelete(c *gin.Context) {
	id, ok := c.Params.Get("id")
	if !ok {
		c.JSON(201, gin.H{"error": "无效的id"})
		return
	} else {
		err := repository.DeleteProductByID(id)
		if err != nil {
			c.JSON(202, gin.H{"error": err.Error()})
			return
		} else {
			c.JSON(http.StatusOK, gin.H{"msg": "Success"})
			return
		}
	}
}

func UploadImage(c *gin.Context) {
	product_id := c.PostForm("productID")
	file, _ := c.FormFile("file")
	filepath := "static/upload/product/"+product_id + ".png"
	if err := c.SaveUploadedFile(file, filepath); err != nil {
		c.JSON(201, "failed")
		return
	}else{
		product,_ := repository.GetProductByID(product_id)
		product.Image = filepath
		err := repository.UpdateOneProduct(product)
		if err!=nil {
			c.JSON(202, "failed")
			return
		}else {
			c.JSON(200, "Success")
			return
		}
	}
}
